//
//  CFRMTodayRecipeController.swift
//  CrazyFatReducingMeal
//
//  Created by iOS-Czz on 2023/11/8.
//

import UIKit
import BRPickerView
class CFRMTodayRecipeController: CFRMBaseViewController,UITextFieldDelegate {
    
    @IBOutlet var daysLabel: UILabel!
    @IBOutlet var currentLabel: UILabel!
    @IBOutlet var dateLabel: UILabel!
    @IBOutlet var breakfastLabel: UILabel!
    @IBOutlet var breakfastStatusLabel: UILabel!
    @IBOutlet var breakfastCompleteIcon: UIImageView!
    
    @IBOutlet var lunchLabel: UILabel!
    @IBOutlet var lunchStatusLabel: UILabel!
    @IBOutlet var lunchCompleteIcon: UIImageView!
    
    @IBOutlet var dinnerLabel: UILabel!
    @IBOutlet var dinnerStatusLabel: UILabel!
    @IBOutlet var dinnerCompleteIcon: UIImageView!
    @IBOutlet var dataLabel: UILabel!
    var receipe = ""
    var currentModel = CFRMRecipeHistoryModel()
    lazy var viewModel: CFRMRecipeViewModel = {
        return CFRMRecipeViewModel()
    }()
    lazy var historyModel: CFRMRecipeHistoryViewModel = {
        return CFRMRecipeHistoryViewModel()
    }()
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        getPageData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.addRightBarButton(imageName: "today_history")
    }
    func getPageData(){
        self.viewModel.getPageData( onSuccess: { [self] models in
            if models.count > 0 {
                setRecipeInfo(models.first!)
            }
            
        })
        
        self.historyModel.loadData { models in
            self.daysLabel.text = String(format: "%ld", models.count + 1)
            let num = NSNumber(value: models.count + 1)
            self.dateLabel.text = Date().getTodayDate() + "  " + self.getNumString(num) + " Fat Reducing Meal"
            if models.count > 0 {
                let model = models.last
                self.dataLabel.text = "weight: \(String(format: "%@", model!.weight))\nBody fat percentage: \(String(format: "%@", model!.percentage))"
                self.currentModel.weight = model!.weight
                self.currentModel.percentage = model!.percentage
            }
        }
    }
    func getNumString(_ num:NSNumber) -> String{
        return NumberFormatter.localizedString(from: num, number: .ordinal)
    }
    func setRecipeInfo(_ model:CFRMRecipeModel) {
        self.receipe = model.name
        self.currentModel.recipeName = model.name
        self.currentLabel.text = "Current Recipe: " + self.receipe
        self.breakfastLabel.text = model.breakfast
        self.lunchLabel.text = model.lunch
        self.dinnerLabel.text = model.dinner
        self.breakfastCompleteIcon.isHidden = true
        self.breakfastStatusLabel.text = "Not eaten"
        self.lunchCompleteIcon.isHidden = true
        self.lunchStatusLabel.text = "Not eaten"
        self.dinnerCompleteIcon.isHidden = true
        self.dinnerStatusLabel.text = "Not eaten"
    }
    override func clockRightBarButtonAction() {
        self.navigationController?.pushViewController(CFRMRecipeHistoryController(), animated: true)
    }
    @IBAction func tapBreakfastAction(_ sender: Any) {
        if !self.currentModel.breakfast {
            self.currentModel.breakfast = true
            self.breakfastCompleteIcon.isHidden = false
            self.breakfastStatusLabel.text = "Finished"
        }
    }
    @IBAction func tapLunchAction(_ sender: Any) {
        if !self.currentModel.lunch {
            self.currentModel.lunch = true
            self.lunchCompleteIcon.isHidden = false
            self.lunchStatusLabel.text = "Finished"
        }
    }
    @IBAction func tapDinnerAction(_ sender: Any) {
        if !self.currentModel.dinner {
            self.currentModel.dinner = true
            self.dinnerCompleteIcon.isHidden = false
            self.dinnerStatusLabel.text = "Finished"
        }
    }
    @IBAction func clickSwitchRecipeAction(_ sender: Any) {
        var names:[String] = []
        for model in self.viewModel.dataSource {
            names.append(model.name)
        }
        let pickerView = BRStringPickerView(pickerMode: .componentSingle)
        let style = BRPickerStyle(themeColor: THEM_COLOR)
        style.language = "en"
        pickerView.pickerStyle = style
        pickerView.dataSourceArr = names
        pickerView.resultModelBlock = { [self] result in
            let selectIndex  = result?.index
            let model = self.viewModel.dataSource[selectIndex!]
            self.currentModel = CFRMRecipeHistoryModel()

            self.receipe = model.name
            setRecipeInfo(model)
        }
        pickerView.show()
    }
    @IBAction func clickEndAction(_ sender: Any) {
        let endView:CFRMEndTodayAlertView = Bundle.main.loadNibNamed("CFRMEndTodayAlertView", owner: self, options: nil)?.first as! CFRMEndTodayAlertView
        endView.frame = keyWindow!.frame
        self.currentModel.date = Date().toString(format: "MM/dd")
        endView.model = self.currentModel
        endView.clickSaveBlock = {
            self.currentModel = CFRMRecipeHistoryModel()
            self.breakfastCompleteIcon.isHidden = true
            self.breakfastStatusLabel.text = "Not eaten"
            self.lunchCompleteIcon.isHidden = true
            self.lunchStatusLabel.text = "Not eaten"
            self.dinnerCompleteIcon.isHidden = true
            self.dinnerStatusLabel.text = "Not eaten"
            self.getPageData()
        }
        keyWindow?.addSubview(endView)
    }
    
}
